package com.etc.layuisys.controller;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.etc.layuisys.entity.Users;
import com.etc.layuisys.service.UsersService;
import com.etc.layuisys.utils.CommonMessage;
import com.etc.layuisys.utils.PageData;

@Controller
@RequestMapping(value = "usersapi")
public class UsersController {

	// 注入一个UsersService
	@Resource(name = "usersService")
	private UsersService usersService;

	/**
	 * 查询方法 根据参数返回请求数据，ajax请求
	 * 
	 * @param page
	 * @param limit
	 * @param content
	 * @return
	 */
	@RequestMapping(value = "listUsers", method = RequestMethod.GET)
	@ResponseBody
	public PageData<Users> listUsers(Integer page, Integer limit, String content) {
		if (content == null) {
			content = "";
		}

		PageData<Users> pd = usersService.getUsersByPage(page, limit, content);
		// 返回json格式的对象
		pd.setCode(0);
		pd.setMsg("请求成功");
		return pd;
	}

	@RequestMapping(value = "updateUser",method=RequestMethod.PUT)
	@ResponseBody
	public CommonMessage updateUser(@RequestBody Users user) {
		
		boolean flag = usersService.updateUser(user);

		System.out.println("updateUser:" + user);

		CommonMessage cm = new CommonMessage(flag ? "修改成功" : "修改失败");

		return cm;
	}
	
	
	@RequestMapping(value = "deleteUser/{userId}",method=RequestMethod.DELETE)
	@ResponseBody
	public CommonMessage deleteUser(@PathVariable(value="userId") int userId) {
		
		boolean flag = usersService.DeleteUser(userId);

		System.out.println("deleteUser:" + userId);

		CommonMessage cm = new CommonMessage(flag ? "删除成功" : "删除失败");

		return cm;
	}
	
	
	@RequestMapping(value = "addUser")
	@ResponseBody
	public CommonMessage addUser(@RequestBody Users user) {
		
		boolean flag = usersService.addUsers(user);

		System.out.println("addUser:" + user);

		CommonMessage cm = new CommonMessage(flag ? "添加成功" : "添加失败");

		return cm;
	}

}
